package qeug.shsh.opplugin;

import kd.bos.bill.BillShowParameter;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.FormMetadataCache;
import kd.bos.form.ShowType;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.fi.bcm.fel.common.NumberUtil;
import org.jetbrains.annotations.NotNull;
import java.util.EventObject;
import java.util.HashSet;
import java.util.Set;

public class EvectionApplyListPlugin extends AbstractListPlugin {

    /**
     * 出差申请单列表插件
     */
    private static String RECORD = "qeug_approval_record";//查看审批记录

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addItemClickListeners("tbmain");//监听按钮
    }

    public void beforeItemClick(BeforeItemClickEvent evt) {
        super.beforeItemClick(evt);
        Set<Long> setIds = getSelectOrderIds(this.getSelectedRows());
        if (evt.getItemKey().equals(RECORD)) {
            if (setIds.size() == 0) {
                this.getView().showTipNotification("请选择要执行的数据。");
                return;
            }
            DynamicObject[] receiveApplications = BusinessDataServiceHelper.load("er_tripreqbill", "billno", new QFilter[]{new QFilter("id", "in", setIds)});
            for (DynamicObject receiveApplication : receiveApplications) {
                String bill = receiveApplication.getString("billno");//单据编号
                QFilter[] qFilter = new QFilter[]{
                        new QFilter("billno", QCP.equals, bill)};
                DynamicObject inventory = BusinessDataServiceHelper.loadSingle("qeug_auditlog", "billno", qFilter);
                if (inventory != null) {
                    String pkId = inventory.getString("id");
                    BillShowParameter showParameter = new BillShowParameter();
                    showParameter.setFormId("qeug_auditlog");
                    showParameter.getOpenStyle().setShowType(ShowType.Modal); //打开方式
                    showParameter.setPkId(pkId);
                    if (showParameter.getFormConfig() == null) {
                        showParameter.setFormConfig(FormMetadataCache.getFormConfig("qeug_auditlog"));
                    }
                    showParameter.getFormConfig().setHeight(new LocaleString("650"));
                    showParameter.getFormConfig().setWidth(new LocaleString("1200"));
                    getView().showForm(showParameter);
                } else {
                    this.getView().showErrorNotification(bill + "无审批记录！");
                }
            }
        }
    }

    @NotNull
    private Set<Long> getSelectOrderIds(@NotNull ListSelectedRowCollection selectedRows) {
        Set<Long> setIds = new HashSet<>();
        for (ListSelectedRow selectedRow : selectedRows) {
            setIds.add(NumberUtil.toLong(selectedRow.getPrimaryKeyValue()));
        }
        return setIds;
    }

}
